package utilities;

import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Vector;

import org.json.simple.JSONArray;
import org.json.simple.JSONObject;
import org.json.simple.parser.JSONParser;
import org.json.simple.parser.ParseException;


/**
 * Parser de archivo para los patrones.json
 * @author ELTIO
 */
public class PatternsInfoLoader {
	
	public final static String PATTERN_NAME = "name";
	public final static String PATTERN_CATEGORY = "category";
	public final static String PATTERN_DESCRIPTION = "description";
	public final static String PATTERN_IMAGE_PATH = "image_path";
	public final static String PATTERN_ELEMENTS = "elements";
	public final static String PATTERN_ELEMENT = "element";
	public final static String PATTERN_FEEDBACK_FILE_PATH = "feedback_file_path";
	
	private static JSONParser parser = new JSONParser();
	
	/**
	 * Parsea el JSON correspondiente al patron dado como parametro y devuelve los resultados en el hashmap 
	 * @param mapping
	 * @param pattern
	 */
	public static void parseJSON(HashMap<String, Object> mapping, EPatterns pattern) {
		
		try {
			
			JSONObject jsonObj = (JSONObject) parser.parse(new FileReader(System.getProperty("user.dir") + pattern.getPath()));

			mapping.put(PATTERN_NAME, jsonObj.get(PATTERN_NAME).toString());
			mapping.put(PATTERN_CATEGORY, jsonObj.get(PATTERN_CATEGORY).toString());
			mapping.put(PATTERN_DESCRIPTION, jsonObj.get(PATTERN_DESCRIPTION).toString());
			mapping.put(PATTERN_IMAGE_PATH, jsonObj.get(PATTERN_IMAGE_PATH).toString());
			mapping.put(PATTERN_FEEDBACK_FILE_PATH, jsonObj.get(PATTERN_FEEDBACK_FILE_PATH).toString());
			
			JSONArray patternElements = (JSONArray) jsonObj.get(PATTERN_ELEMENTS);
			Iterator<JSONObject> iterator = patternElements.iterator();
			
			JSONObject line = null;
			Vector<String> elementNames = new Vector<String>();
			
			while(iterator.hasNext()) {
				line = iterator.next();
				elementNames.add(line.get(PATTERN_ELEMENT).toString());
			}
			
			mapping.put(PATTERN_ELEMENTS, elementNames);
			
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		} catch (ParseException e) {
			e.printStackTrace();
		}

     }
	
	/**
	 * Retorna una propiedad en particular para el patron dado como parametro
	 * @param pattern
	 * @param property
	 * @return
	 */
	public static Object getPatternProperty (EPatterns pattern, String property) {
		HashMap<String, Object> mapping = new HashMap<String, Object>();
		parseJSON(mapping,pattern);
		return mapping.get(property);
	}
	
	public static void main(String[] args) {

		// Main para testear la clase
		
		HashMap<String, Object> mapping = new HashMap<String, Object>();
		
		parseJSON(mapping,EPatterns.Adapter);
		
		System.out.println("PATTERN_NAME: "+mapping.get("name"));
		System.out.println("PATTERN_CATEGORY: "+mapping.get("category"));
		System.out.println("PATTERN_DESCRIPTION: "+mapping.get("description"));
		System.out.println("PATTERN_IMAGE_PATH: "+mapping.get("image_path"));

		Vector<String> elements = (Vector<String>) mapping.get("elements");
		
		for (Enumeration<String> e=elements.elements(); e.hasMoreElements();) {
			String elem = e.nextElement();
			System.out.println("ELEMENT: "+elem);
		}
	
	}
	
	
}